<?php
namespace App\Services;

use Ixudra\Curl\Facades\Curl;
use App\Services\Common;
use DB;

class Store{

	public function addStoreIncrement($sId, $column, $flow){
		
		DB::table('store')->where('sId', $sId)->increment($column, $flow);
	}
	
	// 添加钱包流水
	public function addWalletTrade($wcusKey, $wtObj, $wtObjId, $wtAbstract, $createUser, $uId, 
									$wtChangedMoney, $wtMemo='', $wtCost=false, $wtProfit=false, $wtOrderCode=''){
		
		$host = config('admin.dataApiHost');							
		$walletUrl = $host."/wallet/addTrade";
		$walletData = array();
		$walletData['wcusKey'] = $wcusKey;
		$walletData['wtObj'] = $wtObj;
		$walletData['wtObjId'] = $wtObjId;
		$walletData['wtAbstract'] = $wtAbstract;
		$walletData['createUser'] = $createUser;
		$walletData['uId'] = $uId;
		$walletData['wtChangedMoney'] = $wtChangedMoney;
		$walletData['wtOrderCode'] = $wtOrderCode;
		if ($wtCost !== false){
			$walletData['wtCost'] = $wtCost;
		}
		if ($wtProfit !== false){
			$walletData['wtProfit'] = $wtProfit;
		}
		
		Curl::to($walletUrl)->withData($walletData)->post();
	}
	
	// 添加日统计
	public function addStatsDay($scKey, $sdObjId, $sdName, $sdValue, $sdMemo, $sdKey=''){
		
		$host = config('admin.dataApiHost');
		$statsUrl = $host."/statistics/dayAdd";
		
		$statsData = array();
		$statsData['scKey'] = $scKey;
		$statsData['sdObjId'] = $sdObjId;
		$statsData['sdName'] = $sdName;
		$statsData['sdValue'] = $sdValue;
		$statsData['sdMemo'] = $sdMemo;
		$statsData['sdKey'] = $sdKey;
		
		Curl::to($statsUrl)->withData($statsData)->post();
	}
	
	// 分账
	public function shareBonus($sId, $month, $sbTransferMoney, $createUser){
		
		$sInfo = DB::table('store')->where('sId', $sId)->first();
		$statsInfo = DB::table('stats_month')
						->where('smName', 'profit')
						->where('scId', $sId)
						->where('smObjId', $sInfo->sCode)
						->where('smMonth', $month)
						->first();
		
		$shareholderList = DB::table('admin_users')
							->where('sId', $sId)
							->where('auStock', '>' ,0)
							->get();
		
		$sbInfo = DB::table('share_bonus')->where('sbMonth', $month)->first();
		if (!empty($sbInfo)){
			return '操作失败！本月利润已分红';
		}
		
		$sbShareMoney = $statsInfo->smValue - $sbTransferMoney;
		
		$sbId = DB::table('share_bonus')->insertGetId([
			'sbMonth' => $month,
			'sId' => $sId,
			'sbShareMoney' => $sbShareMoney,
			'sbTransferMoney' => $sbTransferMoney,
			'createUser' => $createUser
		]);

//		$this->addWalletTrade($sId, 'shareBonus', $sbId, '利润保留', $createUser,
//									$sInfo->sCode, $sbTransferMoney, '', 0, $sbTransferMoney);

		$nextMonth = date('Y-m', strtotime($month . ' +1 month'));
		// 添加下月利润统计
		$Common = new Common();
		$Common->addStats($sId, 'profit', $sbTransferMoney, $sInfo->sCode, $nextMonth.'-01');
		
		
		$this->addWalletTrade($sId, 'shareBonus', $sbId, '股东分红', $createUser,
									$sInfo->sCode, $sbShareMoney, '');
		
		foreach($shareholderList as $shareholder){
			$shareMoney = $sbShareMoney * $shareholder->auStock / 100;
			$slId = DB::table('share_log')->insertGetId([
				'sId' => $sId,
				'sbMonth' => $month,
				'auCode' => $shareholder->auCode,
				'alMoney' => $shareMoney,
				'createUser' => $createUser
			]);
			
			$this->addWalletTrade($sId, 'shareBonus', $slId, '店铺分红', $createUser,
									$shareholder->auCode, $shareMoney, '');
		}
		
		return true;
	}
	
	// 添加卡项服务统计
	public function addCardServiceStats($sId, $auCode, $sdMemo=""){
		
		$sInfo = DB::table('store')->where('sId', $sId)->first();
		// 统计个人
		$this->addStatsDay($sId, $auCode, 'cardService', 1, $sdMemo);
		// 统计店铺
		$this->addStatsDay($sId, $sInfo->sCode, 'cardService', 1, $sdMemo);
	}
	
	// 获取标签
	public function getUserTags($sId){
		
		$list = DB::table('store_params')
					->where('sId', $sId)
					->where('spType', 'tags')
					->pluck('spKey', 'spId');
		
		return $list;
	}
	
	// 取消出库
	public function channelGoodsOut($sgoId, $createUser){

		// 获取出库单信息
		$sgoInfo = DB::table('store_goods_out')->where('sgoId', $sgoId)->first();
		if (empty($sgoInfo)){
			return '系统错误：找不到该出库记录';
		}
		
		if ($sgoInfo->sgoIsCancel == 1){
			return '错误：无法重复取消出库单';
		}
		
		// 获取店铺信息
		$sInfo = DB::table('store')->where('sId', $sgoInfo->sId)->first();
		
		// 取消店铺统计
		$this->cancelStats($sgoInfo->sId, $sgoInfo->sgoOrderId);
		
		// 获取钱包相关数据
		$wtList = DB::table('wallet_trade')
					->leftJoin('wallet_account', 'wallet_trade.waId', '=', 'wallet_account.waId')
					->where('wallet_trade.wcusId', $sgoInfo->sId)
					->where('wallet_trade.wtObjId', $sgoInfo->sgoOrderId)
					->get();
		foreach($wtList as $wtInfo){
			// 反向记录钱包金额
			$this->addWalletTrade($wtInfo->wcusId, $wtInfo->wtObj, $wtInfo->wtObjId, '取消出库，金额冲账', $createUser, $wtInfo->uId, 
									-1*$wtInfo->wtChangedMoney, '出库单取消', -1*$wtInfo->wtCost, -1*$wtInfo->wtProfit);
		}
		
		// 获取相关出库单列表
		$sgoList = DB::table('store_goods_out')->where('sId', $sgoInfo->sId)->where('sgoOrderId', $sgoInfo->sgoOrderId)->get();
		foreach($sgoList as $sgoInfo){
			// 获取商品信息
			$sgInfo = DB::table('store_goods')->where('sgId', $sgoInfo->sgId)->first();
			
			// 增加库存数据 //////////////////////////////////////////////////////////////////////////////////////////
			DB::table('store_goods')->where('sgId', $sgoInfo->sgId)->increment('sgNum', $sgoInfo->sgoSaleNum);

			// 不为卡包修改入库单剩余量
			if ($sgInfo->sgIsTimeCard == 0){
				// 增加入库单数据
				$sgiInfo = DB::table('store_goods_in')
							->where('sId', $sgoInfo->sId)
							->where('sgiBatchCode', $sgoInfo->sgiBatchCode)
							->where('sgId', $sgoInfo->sgId)
							->first();
				DB::table('store_goods_in')->where('sgiId', $sgiInfo->sgiId)->increment('sgiSurplus', $sgoInfo->sgoSaleNum);
			}
			// 增加库存数据 //////////////////////////////////////////////////////////////////////////////////////////
			
			// 个人消费总计计算
			$host = config('admin.dataApiHost');
	    	$addExpendUrl = $host."/member/addExpend";
	    	$addData = array();
	    	$addData['mcKey'] = $sInfo->sId;
	    	$addData['muCode'] = $sgoInfo->muId;
	    	$addData['addMoney'] = -1*$sgoInfo->sgoSalePrice;
	
	    	Curl::to($addExpendUrl)->withData($addData)->post();

//			// 返还会员卡
//	    	if ($sgoInfo->sgoPayWay == "会员卡"){
//	    		$this->addWalletTrade($sInfo->sId, 'member', $sgoInfo->sgoOrderId, '取消出库，金额返还', $createUser, $sgoInfo->muId, $sgoInfo->sgoSalePrice, '出库单取消');
//	    	}
	    	
	    	// 获取钱包相关数据
			$wtList = DB::table('wallet_trade')
						->leftJoin('wallet_account', 'wallet_trade.waId', '=', 'wallet_account.waId')
						->where('wallet_trade.wcusId', $sgoInfo->sId)
						->where('wallet_trade.wtObjId', $sgoInfo->sgoId)
						->get();
			foreach($wtList as $wtInfo){
				// 反向记录钱包金额
				$this->addWalletTrade($wtInfo->wcusId, $wtInfo->wtObj, $wtInfo->wtObjId, '取消出库，金额冲账', $createUser, $wtInfo->uId, 
										-1*$wtInfo->wtChangedMoney, '出库单取消', -1*$wtInfo->wtCost, -1*$wtInfo->wtProfit);
			}
			
			// 分销记录取消标识 
			$disList = DB::table('distribution_bill')
						->where('dcId', $sInfo->sId)
						->where('orderId', $sgoInfo->sgoId)
						->update(['dbIsCancel' => 1]);
						
			// 取消出库相关统计
			$this->cancelStats($sgoInfo->sId, $sgoInfo->sgoId);
		}
		
		// 取消卡项可用次数
	    DB::table('store_member_cards')->where('sgoOrderId', $sgoInfo->sgoOrderId)->where('sId', $sInfo->sId)->update(['smcCanUseNum'=> 0, 'smcMemo' => '出库单取消']);  
		
		// 修改退单标识
		DB::table('store_goods_out')->where('sId', $sInfo->sId)->where('sgoOrderId', $sgoInfo->sgoOrderId)->update(['sgoIsCancel' => 1]);
		
		return true;
	}
	
	// 取消统计
	private function cancelStats($scKey, $sdKey, $memo='出库单取消，统计复原'){
		if (empty($sdKey)){
			return;
		}
		$statsList = DB::table('stats_log')
						->where('scId', $scKey)
						->where('sdKey', $sdKey)
						->get();
						
		foreach($statsList as $statsInfo){
			$this->addStatsDay($statsInfo->scId, $statsInfo->sdObjId, $statsInfo->sdName, -1*$statsInfo->sdValue, $memo, $statsInfo->sdKey);
		}
	}
}

?>